package top.ddandang.blog.controller;


import top.ddandang.blog.bean.GlobalResponse;
import top.ddandang.blog.model.query.PageBean;
import top.ddandang.blog.model.vo.MessageRequestVo;
import top.ddandang.blog.service.MessageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author D
 * @since 2020-06-24
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private MessageService messageService;

    @PostMapping("/insertMessage")
    public GlobalResponse insertMessage(@RequestBody MessageRequestVo messageRequestVo) {
        messageService.insertMessage(messageRequestVo);
        return GlobalResponse.success().message("留言成功!");
    }

    @GetMapping("/listMessageVo")
    public GlobalResponse listMessageVo(int pageNo, int pageSize) {
        PageBean pageBean = new PageBean(pageNo, pageSize, 0);
        Map<String, Object> map = messageService.listMessageVo(pageBean);
        if(map.isEmpty()) {
            return GlobalResponse.failed();
        }else {
            return GlobalResponse.success().data(map);
        }
    }

    @DeleteMapping("/deleteMessage")
    public GlobalResponse deleteMessage(Integer messageId) {
        int flag = messageService.deleteMessage(messageId);
        if(flag == 1) {
            return GlobalResponse.success().message("删除成功");
        }else {
            return GlobalResponse.failed().message("删除失败！");
        }
    }

    @PostMapping("/updateTopping")
    public GlobalResponse updateTopping (Integer messageId, Boolean top) {
        int flag = messageService.updateTopping(messageId, top);
        if(flag == 1) {
            return GlobalResponse.success().message("更新成功");
        }else {
            return GlobalResponse.failed().message("更新失败！");
        }
    }
}

